package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;

import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.shop.ShopAddressSaveBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.ztgf.shopping.models.service.shop.IShopAddressService;
import com.ztgf.shopping.models.bo.shop.ShopAddressBO;
import com.ztgf.shopping.models.dto.shop.ShopAddressDTO;

import java.util.Arrays;

/**
 * 用户常用地址表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-27 11:11:21
 */

@RestController
@RequestMapping("/api/shopAddress")
@Slf4j
@Api(value = "ShopAddressApiController",tags = "用户常用地址表Api接口")
public class ShopAddressApiController {
    @DubboReference
    private IShopAddressService shopAddressService;

    @ApiOperation(value = "添加用户常用地址表",notes = "添加用户常用地址表")
    @PostMapping(value = "/insert")
    public Result insert( ShopAddressSaveBO saveBO) {
        if (StringUtils.isEmpty(saveBO.getAddress()) || StringUtils.isEmpty(saveBO.getName()) || StringUtils.isEmpty(saveBO.getMobile())){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        if (saveBO.getShopId() == null || saveBO.getAreaId() == null){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        boolean flag = shopAddressService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改用户常用地址表",notes = "修改用户常用地址表")
    @PostMapping("/update")
    public Result update( ShopAddressSaveBO saveBO){
        return new Result<>(shopAddressService.updateAddress(saveBO));
    }

    @ApiOperation(value = "删除用户常用地址表",notes = "删除用户常用地址表")
    @ApiImplicitParam(name = "id",value = "用户常用地址表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = shopAddressService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除用户常用地址表",notes = "批量删除用户常用地址表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        shopAddressService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopAddressDTO> get(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopAddressDTO dto = shopAddressService.selectById(id);

        return new Result<>(dto);
    }

    @PostMapping("/updateDefault")
    @ApiOperation(value = "更新默认地址",notes = "更新默认地址")
    public Result updateDefault(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        boolean flag = shopAddressService.updateDefault(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "用户常用地址表列表",notes = "用户常用地址表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopAddressDTO>> queryPageList(ShopAddressBO bo){
       Wrapper<ShopAddressDTO> wrapper = new EntityWrapper<>();
        if (bo.getShopId() != null){
            wrapper.eq("shop_id", bo.getShopId());
        }

        if (bo.getType() != null){
            wrapper.eq("type", bo.getType());
        }

        if (bo.getKeyword() != null){
            wrapper.andNew().like("name", bo.getKeyword()).or().like("mobile", bo.getKeyword()).or().like("complete_address", bo.getKeyword());
        }

        wrapper.orderBy("create_time", false);//降序

        Page<ShopAddressDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shopAddressService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
